import router from './router.js'
import { getToken } from './utils/auth'

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach((to, from, next) => {

  // determine whether the user has logged in
  const hasToken = getToken()
  console.log(hasToken)
  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/home' })
    }else{
      next()
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
        next(); // in the free login whitelist, go directly
    } else {
        next('/login'); // 重定向到登录页面
    }
  }
})

